package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.OrderDetailListBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/10/18.
 */

public class OrderDetailAdapter extends BaseAdapter {
	private List<OrderDetailListBean.OrderDetailBean> list = new ArrayList<>();
	private Context mContext;
	private int mHeight;
	private onMoreClickListener moreClickListener;

	public OrderDetailAdapter(List<OrderDetailListBean.OrderDetailBean> list , Context mContext , int mHeight ) {
		this.list = list;
		this.mContext = mContext;
		this.mHeight = mHeight;
	}

	public void setOnMoreClickListener(onMoreClickListener moreClickListener) {
		this.moreClickListener = moreClickListener;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.contract_f_item_order_details, null);
			holder.mNameTv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.mMoneyTv = (TextView) convertView.findViewById(R.id.money_tv);
			holder.mWeekPayTv = (TextView) convertView.findViewById(R.id.weekpay_tv);
			holder.mAlipayTv = (TextView) convertView.findViewById(R.id.alipay_tv);
			holder.mCountBtn = (TextView) convertView.findViewById(R.id.count_btn);
			holder.mMoreIv = (ImageView) convertView.findViewById(R.id.more_iv);
			holder.mLogoIv = (ImageView) convertView.findViewById(R.id.logo_iv);
			holder.mErrorTipTv = (TextView) convertView.findViewById(R.id.error_tip_tv);
			final View finalConvertView = convertView;
			holder.mMoreIv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					moreClickListener.onMore(finalConvertView,position);
				}
			});
			holder.mCountBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					moreClickListener.onEditCount(v ,position);
				}
			});
			holder.mNameTv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					moreClickListener.onNameClick(v , position);
				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (list.size() > 0) {
			if (StringUtils.isNoneNullString(list.get(position).getItemName())) {
				holder.mNameTv.setText(list.get(position).getItemName());

			}
			if (StringUtils.isNoneNullString(list.get(position).getItemUrl())) {
				ImageloaderUtils.displayRoundImg(list.get(position).getItemUrl() , holder.mLogoIv);
			}
			if (StringUtils.isNoneNullString(list.get(position).getRsQty())) {
				holder.mCountBtn.setText("x" + list.get(position).getRsQty());
			}
			if (StringUtils.isNoneNullString(list.get(position).getPbPrice())) {
				holder.mMoneyTv.setText("¥" + list.get(position).getPbPrice());
			}
			if (StringUtils.isNoneNullString(list.get(position).getStatus())) {
				if (list.get(position).getStatus().equals("0")) {
					holder.mErrorTipTv.setVisibility(View.GONE);
				} else if (list.get(position).getStatus().equals("704001")) {
					holder.mErrorTipTv.setText("当前商品库存不足");
				} else if (list.get(position).getStatus().equals("704002")) {
					holder.mErrorTipTv.setText("该商品已下架");
				} else if (list.get(position).getStatus().equals("704003")) {
					holder.mErrorTipTv.setText("当前商品可售库存为0");
				}

			}
			if (StringUtils.isNoneNullString(list.get(position).getPayWay())) {
				String payWay = list.get(position).getPayWay();
				String payZh = "";
				if (payWay.equals("aliPay")) {
					payZh = "支付宝";
				} else if (payWay.equals("bank")) {
					payZh = "银行卡";
				} else if (payWay.equals("offline")) {
					payZh = "线下付";
				} else if (payWay.equals("wechatPay")) {
					payZh = "微信付";
				}
				holder.mAlipayTv.setText(payZh);
			} else {
				holder.mAlipayTv.setVisibility(View.GONE);
			}
			if (StringUtils.isNoneNullString(list.get(position).getPayType())) {
				String payType = list.get(position).getPayType();
				if (payType.equals("orderPay")) {
					holder.mWeekPayTv.setText("下单付");
				} else {
					if (payType.equals("month")) {
						holder.mWeekPayTv.setText("月付");
					} else if (payType.equals("week")) {
						holder.mWeekPayTv.setText("周付");
					}
				}
			} else {
				holder.mWeekPayTv.setVisibility(View.GONE);
			}


		}
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, mHeight / 4);
		convertView.setLayoutParams(lp);
		return convertView;
	}

	public class ViewHolder {
		private TextView mNameTv, mMoneyTv , mWeekPayTv , mAlipayTv , mErrorTipTv;
		private TextView mCountBtn;
		private ImageView mMoreIv;
		private ImageView mLogoIv;
	}

	public interface onMoreClickListener {
		void onMore(View view , int position);

		void onEditCount(View view ,int position);

		void onNameClick(View view, int position);
	}

}
